package com.mls.fivedistrict.controller.backend;

import com.baomidou.mybatisplus.mapper.Condition;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.Role;
import com.mls.fivedistrict.server.RoleService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;


/**
 * Created by nanji on 2018/3/20.
 */
@RestController
@RequestMapping("/api/manager")
public class RoleController {
    @Autowired
    RoleService roleService;


    @GetMapping("/v1/roles")
    public ServiceResponse getRoleList() {
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        List list = roleService.selectList(pageFilter.getWrapper());
        PageInfo pageInfo = new PageInfo<>(list, 5);
        return ServiceResponse.createSuccessResponse(pageInfo);
    }

    /**
     * 获取所有角色
     *
     * @return
     */
    @GetMapping("/v1/role/all")
    public ServiceResponse getAllRole() {
        List<Role> roleList = roleService.selectList(Condition.create().eq("is_delete", false).eq("is_enable", true));
        return ServiceResponse.createSuccessResponse(roleList);
    }

    @GetMapping("/v1/role/{roleId}")
    public ServiceResponse getRoleById(@PathVariable("roleId") Long roleId) {
//        Role role = roleService.selectAndPermissions(roleId);
        Role role=roleService.selectById(roleId);
        return ServiceResponse.createSuccessResponse(role);
    }

    @PostMapping("/v1/role")
    public ServiceResponse addRole(@RequestBody Role role) {
        BeanValidator.check(role);
        boolean insert = roleService.insertAndPermission(role);
        if (insert) {
            return ServiceResponse.createSuccessResponse("创建成功");
        }
        return ServiceResponse.createErrorResponse("创建失败");
    }

    @PutMapping("/v1/role/{roleId}")
    public ServiceResponse editRole(@RequestBody Role role, @PathVariable("roleId") Long roleId) {
        Optional.ofNullable(roleId).orElseThrow(() -> new CustomerException("id不能为空"));
        Optional.ofNullable(role).orElseThrow(() -> new CustomerException("角色实体不能为空"));
        role.setId(roleId);
        boolean insert = roleService.updateAndPermission(role);
        if (insert) {
            return ServiceResponse.createSuccessResponse("操作成功");
        }
        return ServiceResponse.createErrorResponse("操作失败");
    }

    @DeleteMapping("/v1/role/{roleId}")
    public ServiceResponse deleteUser(@PathVariable(name = "roleId") Long roleId) {
//        boolean b = roleService.deleteAndPermissions(roleId);
        Role role = new Role();
        role.setId(roleId);
        role.setIsDelete(true);
        boolean update = roleService.updateById(role);
        if (update) {
            return ServiceResponse.createSuccessResponse("操作成功");
        } else {
            return ServiceResponse.createErrorResponse("操作失败");
        }
    }
}
